﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Collections;


namespace Dragon.Prototype1.Infrastructure.Controls
{
    public abstract class WebControl
    {
        protected string tagName;
        protected HtmlTextWriterTag tagKey=HtmlTextWriterTag.Unknown;

        public WebControl()
        {
            this.Enabled = true;
        }

        protected virtual string TagName
        {
            get
            {
                if ((this.tagName == null) && (this.TagKey != HtmlTextWriterTag.Unknown))
                {
                    this.tagName = Enum.Format(typeof(HtmlTextWriterTag), this.TagKey, "G").ToLower();
                }
                return this.tagName;
            }
        }

        protected virtual HtmlTextWriterTag TagKey
        {
            get
            {
                return this.tagKey;
            }
        }

        private Dictionary<string, string> attrs = new Dictionary<string, string>();
        public Dictionary<string, string> Attributes
        {
            get { return attrs; }
        }

        public bool Enabled
        {
            get;
            set;
        }

        public int TabIndex
        {
            get;
            set;
        }


        public string CssClass
        {
            get;
            set;
        }

        public string Style
        {
            get;
            set;
        }

        public string ID
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string AccessKey
        {
            get;
            set;
        }

        protected virtual void OnPreRender(HtmlTextWriter writer)
        {

        }

        public virtual void Render(HtmlTextWriter writer)
        {
            this.OnPreRender(writer);
            this.RenderBeginTag(writer);
            this.RenderContents(writer);
            this.RenderEndTag(writer);

        }

        public virtual void RenderContents(HtmlTextWriter writer)
        {
            
        }

        public virtual void RenderBeginTag(HtmlTextWriter writer)
        {
            this.AddAttributesToRender(writer);
            HtmlTextWriterTag tagKey = this.TagKey;
            if (tagKey != HtmlTextWriterTag.Unknown)
            {
                writer.RenderBeginTag(tagKey);
            }
            else
            {
                writer.RenderBeginTag(this.TagName);
            }
        }

        public virtual void RenderEndTag(HtmlTextWriter writer)
        {
            writer.RenderEndTag();
        }

        protected virtual void AddAttributesToRender(HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(this.ID))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);
            }
            if (!string.IsNullOrEmpty(this.Style))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Style, this.Style);
            }
            if (!string.IsNullOrEmpty(this.Name))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Name, this.Name);
            }

            if (!string.IsNullOrEmpty(this.CssClass))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
            }
            if (!string.IsNullOrEmpty(this.AccessKey))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, this.AccessKey);
            }

            if (!this.Enabled)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            }
            if (TabIndex > 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
            }

            foreach (KeyValuePair<string, string> attr in attrs)
            {
                writer.AddAttribute(attr.Key, attr.Value);
            }

        }
    }
}
